﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Utils;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.XtraPrinting;

namespace XAF1.Module.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class CustomizeExportController : ViewController
    {
        public CustomizeExportController()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
            TargetViewType = ViewType.ListView;
        }
        private ExportController exportController;
        protected override void OnActivated()
        {
            base.OnActivated();
            // Perform various tasks depending on the target View.
            exportController = Frame.GetController<ExportController>();
            if (exportController != null)
            {
                exportController.CustomGetDefaultFileName += exportController_CustomGetDefaultFileName;
                exportController.CustomExport += CustomExport;
            }
        }
        /// <summary>
        /// 修改默认导出方式中Els和xlsx的sheetName
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void CustomExport(object sender, CustomExportEventArgs e)
        {
            // xls导出方式也修改sheetName
            if (e.ExportTarget == ExportTarget.Xls)
            {
                XlsExportOptions options = e.ExportOptions as XlsExportOptions;
                if (options == null)
                {
                    options = new XlsExportOptions();
                }
                options.SheetName = View.Caption;
                options.ShowGridLines = true;
                e.ExportOptions = options;
            }
            // xlsx导出方式也修改sheetName
            if (e.ExportTarget == ExportTarget.Xlsx)
            {
                XlsxExportOptions options = e.ExportOptions as XlsxExportOptions;
                if (options == null)
                {
                    options = new XlsxExportOptions();
                }
                options.SheetName = View.Caption;
                options.ShowGridLines = true;
                e.ExportOptions = options;
            }
        }
        /// <summary>
        /// 修改默认导出功能的文件名命名方式。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void exportController_CustomGetDefaultFileName(object sender, CustomGetDefaultFileNameEventArgs e)
        {
            //Provide a custom file name
            e.FileName = e.FileName + "_" + DateTime.Now.ToString("MM.dd.yy");
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            // Access and customize the target View control.
        }
        protected override void OnDeactivated()
        {
            // Unsubscribe from previously subscribed events and release other references and resources.
            base.OnDeactivated();
        }
    }
}
